package com.zz.controller;

import com.zz.common.Log;
import com.zz.common.R;
import com.zz.entity.Activity;
import com.zz.entity.Club;
import com.zz.entity.User;
import com.zz.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 个人中心模块
 * @author 朱彦燕
 */
@RestController
@Slf4j
@RequestMapping("/personal")
@CrossOrigin
@Validated
public class PersonalController {

    @Autowired
    private UserService userService;

    @Autowired
    private ClubLeaderService clubLeaderService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private AttendedService attendedService;

    /**
    * 修改个人信息,学号不允许修改
     * 测试成功，但是图片传输那块有待考究
     * 朱彦燕
    * */
    @PutMapping("/update")
    @CacheEvict(value = "personalCache",allEntries = true)
    @Log(title = "普通用户：修改个人信息", dataLocationType = "body")
    public R<String> updateInfo(@RequestBody User user, HttpServletRequest request){
        return userService.update(user, request);
    }

    /**
     * 个人信息展示,前端不需要传参数
     * 此接口已经测试成功
     * @param request
     * 朱彦燕
    * */
    @GetMapping("/info")
    //@Cacheable(value="personalInfoCache",key = "#request.getUserPrincipal()",sync = true)
    @Log(title = "普通用户：个人中心信息展示")
    public R<User> info(HttpServletRequest request){
        return userService.info(request);
    }

    /**
     * 展示用户已参加社团,前端不需要传参数
     * 测试成功
     * 朱彦燕
    * */
    @GetMapping("/club")
    @Cacheable(value="personalClubCache",key="'personalClub'+#request.getUserPrincipal()",sync = true)
    @Log(title = "普通用户：个人中心，展示用户已参加社团")
    public R<List<Club>> club(HttpServletRequest request){
        return userService.club(request);
    }

    /**
    * 个人中心修改密码
    * 朱彦燕
    * 测试成功
    * */
    @PutMapping("/changePassword")
    @CacheEvict(value = "personalCache",allEntries = true)
    @Log(title = "普通用户：个人中心，修改密码", dataLocationType = "body")
    public R<String> changePassword(@RequestBody @Valid Map<String,String> map, HttpServletRequest request){
        return userService.changePassword(map, request);
    }

    /**
     * 普通用户申请成为社团负责人
     * */
    @PostMapping("/applyLeader")
//    @CacheEvict(value = "personalCache",allEntries = true)
    @Log(title = "申请成为社团负责人", dataLocationType = "link")
    R<String> applyLeader(@RequestParam @NotNull(message = "社团编码不能为空") int clubId, HttpServletRequest request){
        return clubLeaderService.apply(clubId, request);
    }

    /**
     * 用户查看已参加未举办活动
     * */
    @GetMapping("/attendedUnstart")
    @Log(title = "用户查看已参加未举办活动", dataLocationType = "link")
    R<List<Activity>> attendedUnstart(HttpServletRequest request){
        return attendedService.  attendedUnstart(request);
    }

    /**
     * 用户查看已参加已结束活动
     * */
    @GetMapping("/attendedEnd")
    @Log(title = "用户查看已参加已结束活动",dataLocationType = "link")
    R<List<Activity>> attendedEnd(HttpServletRequest request){
        return attendedService.attendedEnd(request);
    }

    /**
     * 用户查看已参加已结束活动(没有填写调查问卷)
     * */
    @GetMapping("/attendedEndUnQ")
    @Log(title = "用户查看已参加已结束活动(没有填写调查问卷)", dataLocationType = "link")
    public R<List<Activity>> attendedEndUnQ(HttpServletRequest request){
        return attendedService.attendedEndUnQ(request);
    }
}
